// Muduo - A reactor-based C++ network library for Linux
// Copyright (c) 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//   * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//   * Neither the name of Shuo Chen nor the names of other contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <clusnet/base/Thread.h>
#include <clusnet/base/CurrentThread.h>

#include <string>
#include <functional>
#include <unistd.h>
#include <stdio.h>

void mysleep(int seconds)
{
    timespec t = {seconds, 0};
    nanosleep(&t, NULL);
}

void threadFunc()
{
    printf("tid=%d\n", clusnet::base::CurrentThread::tid());
}

void threadFunc2(int x)
{
    printf("tid=%d, x=%d\n", clusnet::base::CurrentThread::tid(), x);
}

void threadFunc3()
{
    printf("tid=%d\n", clusnet::base::CurrentThread::tid());
    mysleep(1);
}
 
class Foo
{
    public:
        explicit Foo(double x)
            : x_(x)
        {
        }

        void memberFunc()
        {
            printf("tid=%d, Foo::x_=%f\n", clusnet::base::CurrentThread::tid(), x_);
            mysleep(10);
        }

        void memberFunc2(const std::string& text)
        {
            printf("tid=%d, Foo::x_=%f, text=%s\n", clusnet::base::CurrentThread::tid(), x_, text.c_str());
        }

    private:
        double x_;
};

int main()
{
    if (clusnet::base::CurrentThread::isMainThread())
    {
        printf("mainthread. pid=%d, tid=%d, name=%s\n", ::getpid(), clusnet::base::CurrentThread::tid(), clusnet::base::CurrentThread::t_threadName);
    }
    else
    {
        printf("pid=%d, tid=%d, name=%s\n", ::getpid(), clusnet::base::CurrentThread::tid(), clusnet::base::CurrentThread::t_threadName);
    }

    clusnet::base::Thread t1(threadFunc);
    t1.start();
    printf("t1.tid=%d\n", t1.tid());
    t1.join();

    clusnet::base::Thread t2(std::bind(threadFunc2, 42), "thread for free function with argument");
    t2.start();
    printf("t2.tid=%d\n", t2.tid());
    t2.join();

    Foo foo(87.53);
    clusnet::base::Thread t3(std::bind(&Foo::memberFunc, &foo), "thread for member function without argument");
    t3.start();
    t3.join();

    clusnet::base::Thread t4(std::bind(&Foo::memberFunc2, std::ref(foo), std::string("clusnet")));
    t4.start();
    t4.join();

    {
        clusnet::base::Thread t5(threadFunc3);
        t5.start();
    }
    mysleep(2);

    {
        clusnet::base::Thread t6(threadFunc3);
        t6.start();
        mysleep(2);
    }
    sleep(2);
    printf("number of created threads %d\n", clusnet::base::Thread::numCreated());
    getchar();
    return 0;

}
